import axios from 'axios'

const service = axios.create({
	baseURL: 'http://api.example.com', // 接口的基础路径
	timeout: 5000 // 请求超时时间
})

service.interceptors.request.use(
	config => {
		// 在这里可以对请求头进行设置
		config.headers['Authorization'] = 'Bearer ' + window.localStorage.getItem('token')
		return config
	},
	error => {
		console.log(error)
		return Promise.reject(error)
	}
)

////登录
deng() {
		let _this = this;
		uni.login({
			success(res) {
				_this.denglu(res.code)
			},
			fail() {
				uni.showToast({
					title: '已失效'
				})
			}
		})
	},
	async denglu(res) {
			let msg = await denglu.wxlogin(res)
			let bb = JSON.parse(msg.messageData.data.body)
			console.log(bb.openid);
			let fukuan = await zhifu.zhifu({
				openId: bb.openid,
				price: this.zj * 100
			})
			console.log(fukuan);
			uni.requestPayment({
				...fukuan.messageData.data.orderInfo,
				success(res) {
					console.log('支付成功');
				},
				fail(res) {
					console.log('失败');
				}
			})
		},
		//登录过期处理
		axios.interceptors.request.use(
			config => {
				const token = localStorage.getItem('token')
				// 判断 token 是否存在以及是否过期
				if (token && Date.now() < token.expires) {
					config.headers.authorization = `Bearer ${token.value}`
				} else {
					window.location.href = '/login'
				}
				return config
			},
			error => Promise.reject(error)
		)

export default service